<?php
namespace Admin\Model;
use Think\Model;
class ProductPriceModel extends Model {

	public function index($arr){
		$number = $arr['psize'];
		if( !$number )
			$number = $this->m_psize;
		else 
			$number = $arr['psize'];

		$uinfo = UserModel::loadFromSession();
		$maps['sector_id'] = $uinfo['sector_id']; 
		$company = M("sector")->field("parent_company")->where($maps)->find();
		
		

		$PPrice = M("product_price");
		if( $arr['company']!="" &&  $arr['company']!="null"){
			$where['product.sub_company'] = $arr['company'];
		}else{
			if($uinfo['user_id'] !="1" ){
				if($company['parent_company'] != "1"){
					$where['product.sub_company'] = $company['parent_company'];
				}else{
					$where['product.sub_company'] = $uinfo['sector_id'];
				}
			}
		}
		if($arr['product']!="" && $arr['product']!="null"){
			$where['product.product_id'] = $arr['product'];
		}
		if( $arr['type']!="" && $arr['type'] !="-1"){
			$where['product_price.service_time'] = $arr['type'];
		}

		$count = $PPrice
				->join("product ON product_price.product_id=product.product_id")
				->where($where)
				->count();
		$Page = new \Common\ORG\Page( $count, $number );
		$product_price = $PPrice
			->field("product.name as product_name,product_price.*")
			->join("product ON product_price.product_id=product.product_id")
			->limit($Page->firstRow.','.$Page->listRows )
			->where($where)
			->order("product_price.price_id desc")
			->select(); 
// 
		if($product_price['free_price'] + $product_price['service_price'] > 0){
			$product_price['price'] = $product_price['free_price'] + $product_price['service_price'];
		}
		
		foreach ($product_price as &$value) {
			$value['price'] = $value['free_price'] + $value['service_price'];
		}

		if( $product_price){
			$res['data'] = $product_price;
			$res['page'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $number;
			$res['rcode'] = "1";
		}else{
			$res['rcode'] = "2";
		}
		return $res;
	}
	
	public function product_price_add($arr){
		//添加报价
		$PProduct = M("product_price");
		$Product = M("product");
		$map['service_time'] = $arr['add_service_time'];
		$map['free_price'] = $arr['add_price']-$arr['add_service_price'];
		$price = $arr['add_service_price'];
		$map['service_price'] = $arr['add_service_price'];
		$map['name'] = $arr['add_name'];
		$map['note'] = $arr['add_note'];
		$map['free_time'] = $arr['add_free_time'];
		$map['product_id'] = $arr['product_id'];
		$map['create_time'] = date("Y-m-d H-i-s");
		$product_price = $PProduct ->add($map);

		return $product_price;
	}
	public function eidt($arr){

		$arruinfo = UserModel::loadFromSession();
		$PPrice = M("product_price");

		if(!empty($arr)){
			$uid['product_price.price_id'] = $arr['price_id'];
		
			$res['product_price'] = $PPrice 
								->field("product.product_id,product.name as product_name,product_price.*")
								->join("product ON product_price.product_id = product.product_id")
								->where($uid)
								->find();
		}
					
		$Mu = M("User");
		$map['role_id'] = $arruinfo['role_id'];

		//是否含有管理权限
		$Rights = M("role_rights");

		$rights = $Rights->field("right_id") ->where($map)->select();
		foreach ($rights as $k => $vv) {
			$tId[] = $vv['right_id'];
		}
		$Id = $arruinfo['role_id'];
		$Produc = M("product");
		if(in_array('1', $tId)){
			$type['product_type'] = 0;
			$res['product']= $Produc->field("product_id,name")->where($type)->select();
		}else{
			if($Id['role_id'] =='1' || $Id['role_id'] =='2'|| $Id['role_id'] =='3'){
					$SubId['sub_company'] = $Id['role_id'];
					$SubId['product_type'] = 1;
					$res['product']= $Produc->field("product_id,name")->where($SubId)->select();
	
			}else{
				$Role = M("role");
				$sId['role_id'] = $Id['role_id'];	
				$parent_role = $Role->field("parent_role")->where($sId)->find();
				
				if($parent_role['parent_role'] =='1' || $parent_role['parent_role'] =='2'|| $parent_role['parent_role'] =='3'){
					$SubId['sub_company'] = $parent_role['parent_role'];
					$SubId['product_type'] = 1;
					$res['product']= $Produc->field("product_id,name")->where($SubId)->select();
				}else{
					$zz['role_id'] = $parent_role['parent_role'];
					$role = $Rights->field("parent_role")->where($zz)->find();
					if($role['parent_role'] =='1' || $role['parent_role'] =='2'|| $role['parent_role'] =='3'){
						$SubId['sub_company'] = $role['parent_role'];
						$SubId['product_type'] = 1;
						$res['product']= $Produc->field("product_id,name")->where($SubId)->select();
					}else{
						$res['next'] = "next";
					}
				}		
			}
		}
		//合同期限
		$res['service_time'] = array();
		$res['service_time']['0'] = array("id"=>"12m","time"=>"按年"); 
		$res['service_time']['1'] = array("id"=>"6m","time"=>"按半年");
		$res['service_time']['2'] = array("id"=>"3m","time"=>"按季度");
		$res['service_time']['3'] = array("id"=>"1m","time"=>"按月度");
		return $res;
	}
	public function save_product_price($array){
		//修改product_id
		//return $array;
		$PProduct = M("product_price");

		$map['name'] = $array['save_name'];
		$map['product_id'] = $array['save_product_name'];	
		$map['service_time'] = $array['save_service_time'];
		$map["free_price"] = $array['save_price'] - $array['save_service_price'];
		$map['free_time'] = $array['save_free_time'];
		$map['service_price'] = $array['save_service_price'];
		$map['note'] = $array['save_note'];
		$map['create_time'] = date("Y-m-d H-i-s");
		$where['price_id'] = $array['price_id'];

		$product_price = $PProduct ->where($where)->save($map);

		if($product_price){
			$res['rcode'] = 1;
		}else{
			$res['msg'] = "修改失败!";
		}

		return $res;
	}

	public function Search($arr,$role_id){
		$part = $this->getchildroles($arr,$role_id);
		return $part;
	}

	 public function getchildroles($data,$roleid){
	  	//岗位递归
	  	  static $arrid=array();
	      foreach ($data as $key => $value) {
	         if ($value['role_id'] == $roleid && $roleid != 1) {
	            if($value['parent_role'] > 1){ $arrid[]=$value['parent_role'];}
	            $this->getchildroles($data,$value['parent_role']);
	         }
	      }
	      return array_unique($arrid);//消除重复项
	  }

	public function User(){
		$arruinfo = UserModel::loadFromSession();
		return $arruinfo;
	}
}